package managedbean.FRM.CustomerPayment;

import entity.CRM.Customer;
import entity.CRM.SaleLead;
import entity.FRM.CustomerInvoice;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.CustomerBeanLocal;
import session.stateless.CRM.SaleLeadBeanLocal;
import session.stateless.FRM.CustomerPaymentBeanLocal;

@ManagedBean(name="addCustomerPaymentManagerBean")
@ViewScoped
public class AddCustomerPaymentsManagerBean implements Serializable {
    public AddCustomerPaymentsManagerBean() {}
    
    @EJB
    private CustomerPaymentBeanLocal customerPaymentBeanLocal;
    @EJB
    private SaleLeadBeanLocal saleLeadBeanLocal;
    @EJB
    private CustomerBeanLocal customerBeanLocal;
    
    private Customer selectedCustomer;
    private CustomerInvoice selectedCustomerInvoice;
    private Double paymentAmount;
    private boolean isExisting;
    private boolean isNotExisting;
    private String saleLeadName;
    
    private boolean isSelectedInvoice;
    private String statusMessage;
    private boolean isPaymentValid;
    
    public void initView(PhaseEvent event){
        try
        {
            if(selectedCustomer!=null)
            {
                this.setIsExisting(true);
                this.setIsNotExisting(false);
            }
            else
            {
                this.setIsExisting(false);
                this.setIsNotExisting(true);
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();            
        }
    }

    public Double getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(Double paymentAmount) {
        this.paymentAmount = paymentAmount;
    }

    public boolean isIsPaymentValid() {
        return isPaymentValid;
    }

    public void setIsPaymentValid(boolean isPaymentValid) {
        this.isPaymentValid = isPaymentValid;
    }
    

    public boolean isIsSelectedInvoice() {
        return isSelectedInvoice;
    }

    public void setIsSelectedInvoice(boolean isSelectedInvoice) {
        this.isSelectedInvoice = isSelectedInvoice;
    }

    
    public boolean isIsNotExisting() {
        return isNotExisting;
    }

    public void setIsNotExisting(boolean isNotExisting) {
        this.isNotExisting = isNotExisting;
    }
    
    public boolean isIsExisting() {
        return isExisting;
    }

    public void setIsExisting(boolean isExisting) {        
            this.isExisting= isExisting;                
    }

    public String getSaleLeadName() {
        return saleLeadName;
    }

    public void setSaleLeadName(String saleLeadName) {
        this.saleLeadName = saleLeadName;
    }

    public CustomerInvoice getSelectedCustomerInvoice() {
        return selectedCustomerInvoice;
    }

    public void setSelectedCustomerInvoice(CustomerInvoice selectedCustomerInvoice) {
        this.selectedCustomerInvoice = selectedCustomerInvoice;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }

    public void setSelectedCustomer(Customer selectedCustomer) {
        this.selectedCustomer = selectedCustomer;
    }
    
    public void retrieve()
    {
        if (saleLeadName!=null)
        {
            this.setSelectedCustomer(customerBeanLocal.findCustomerByName(saleLeadName));
        }
    }
    
    public List<String> complete(String query) {  
        List<String> results = new ArrayList<String>();  
        
        List<SaleLead> list = saleLeadBeanLocal.getAllSalesLead();
        List<Customer> list2 = customerBeanLocal.getAllCustomers();
        
        for (int i = 0; i < list.size(); ++i)
        {
            if (list.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list.get(i).getSaleLeadName());
        }
        
        for (int i = 0; i < list2.size(); ++i)
        {
            if (list2.get(i).getSaleLeadName().toLowerCase().contains(query.toLowerCase()))
                results.add(list2.get(i).getSaleLeadName());
        }
        return results;  
    }
    
    public void makePayment(ActionEvent event) throws IOException
    {
        selectedCustomerInvoice = this.getSelectedCustomerInvoice();
        if (selectedCustomerInvoice !=null)
        {
            this.setIsSelectedInvoice(true);
        }        
    }
    
    public void confirmPayment(ActionEvent event) throws IOException
    {
        if (selectedCustomerInvoice !=null)
        {
            System.out.println("test b4 makePayment" + paymentAmount);
            System.out.println("test b5 makePayment" + selectedCustomerInvoice.getCustomerInvoiceAmount());

            if (selectedCustomerInvoice.getCashInAdvance() == null) 
            {
                boolean isValid = customerPaymentBeanLocal.isValid_checkPayment(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                if (isValid) {
                    customerPaymentBeanLocal.createCustomerPayment(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                    statusMessage = "New payment Saved Successfully";
                } else {
                    statusMessage = "Payment failed: Amount is not sufficient";
                }
            }
            else 
            {
                boolean isValid_CIA = customerPaymentBeanLocal.isValid_checkPaymentCIA(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                if (isValid_CIA) {
                    customerPaymentBeanLocal.createCustomerPayment_CIA(selectedCustomerInvoice.getCustomerInvoiceId(), paymentAmount);
                    statusMessage = "New payment Saved Successfully";
                } else {
                    statusMessage = "Payment failed: Amount is not sufficient";
                }
            }
        }
        else
            statusMessage= "Invalid: CustomerInvoice is not found";
        
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));   
        this.setSelectedCustomer(customerBeanLocal.findCustomerById(this.getSelectedCustomer().getRegistrationNumber()));
    }
        
    }
